package utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

public class ConfigItem {

    private int key;

    public ConfigItem() {
    }

    public ConfigItem(int key, String value) {
        this.key = key;
        this.value = value;
    }
    
    public int getKey() {
        return key;
    }

    public void setKey(int key) {
        this.key = key;
    }
    private String value;

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public byte[] getData() throws IOException {
        ByteArrayOutputStream bin = new ByteArrayOutputStream();
        DataOutputStream out = new DataOutputStream(bin);
        out.writeInt(key);
        out.writeUTF(value);

        out.flush();
        return bin.toByteArray();
    }

    public static ConfigItem create(byte[] data) throws IOException {
        ConfigItem item = new ConfigItem();
        ByteArrayInputStream bin = new ByteArrayInputStream(data);
        DataInputStream in = new DataInputStream(bin);

        item.setKey(in.readInt());
        item.setValue(in.readUTF());
        return item;
    }

}
